﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.IntermediateClasses;

namespace ImageProcessing
{
    public partial class PeriodicNoise : Form
    {
        Bitmap originalImage;
        FourierOperations fourierOps;
        public PeriodicNoise(Image image)
        {
            InitializeComponent();
            this.pictureBoxOriginal.Image = image;
            this.originalImage = new Bitmap(image);
        }

        private void btnNoise_Click(object sender, EventArgs e)
        {
            int a = int.Parse(this.txtAmplitude.Text);
            double u0 = double.Parse(this.txtFrequencyX.Text);
            double v0 = double.Parse(this.txtFrequencyY.Text);
            double bX = double.Parse(this.txtPhaseX.Text);
            double bY = double.Parse(this.txtPhaseY.Text);

            Noise noise = new Noise(this.originalImage);
            this.pictureBoxResult.Image = noise.PeriodicNoise(a, u0, v0, bX, bY);
        }

        private void pictureBoxOriginal_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.gpBoxCoordinates.Visible == true)
            {
                this.lblX.Text = Math.Abs(e.X - (this.originalImage.Width / 2)).ToString();
                this.lblY.Text = Math.Abs(e.Y - (this.originalImage.Height / 2)).ToString();
            }
        }

        private void pictureBoxOriginal_MouseClick(object sender, MouseEventArgs e)
        {
            Bitmap image = new Bitmap(this.pictureBoxOriginal.Image);
            Filtering filter = new Filtering(image, 0);
            if (this.txtRadius.Text != "")
            {
                if (this.rdbtnReject.Checked == true)
                {
                    this.pictureBoxOriginal.Image = filter.IdealNotchRejectFilter(this.fourierOps, Math.Abs(e.X - (this.originalImage.Width / 2)), Math.Abs(e.Y - (this.originalImage.Height / 2)), int.Parse(this.txtRadius.Text));
                }
                else
                {
                    this.pictureBoxOriginal.Image = filter.IdealNotchPassFilter(this.fourierOps, Math.Abs(e.X - (this.originalImage.Width / 2)), Math.Abs(e.Y - (this.originalImage.Height / 2)), int.Parse(this.txtRadius.Text));
                }
            }
            else
                if (this.txtBandwidth.Text != "")
                {
                    double distance = Math.Sqrt(Math.Pow(Math.Abs(e.X - (this.originalImage.Width / 2)), 2) + Math.Pow(Math.Abs(e.Y - (this.originalImage.Height / 2)), 2));
                    if (this.rdbtnBandReject.Checked == true)
                    {
                        this.pictureBoxOriginal.Image = filter.IdealBandRejectFilter(this.fourierOps, distance, int.Parse(this.txtBandwidth.Text));
                    }
                    else
                    {
                        this.pictureBoxOriginal.Image = filter.IdealBandPassFilter(this.fourierOps, distance, int.Parse(this.txtBandwidth.Text));
                    }
                }
        }

        private void btnRemoveNoise_Click(object sender, EventArgs e)
        {
            try
            {
                this.groupBox1.Visible = true;
                this.gpBoxCoordinates.Visible = true;
                this.groupBox2.Visible = true;
                Bitmap noisy = new Bitmap(this.pictureBoxResult.Image);
                fourierOps = new FourierOperations(noisy);
                fourierOps.Transform();
                this.pictureBoxOriginal.Image = fourierOps.RedMagnitude();
            }
            catch
            {
                MessageBox.Show("Error during fourier frequency transform operation and magnitude display.");
            }
        }

        private void btnReverseFourier_Click(object sender, EventArgs e)
        {
            Bitmap noisy = new Bitmap(this.pictureBoxOriginal.Image);
            this.pictureBoxResult.Image = fourierOps.ReverseFourier();
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            this.pictureBoxResult.Image = this.pictureBoxOriginal.Image;
        }

        private void btnSaveOriginal_Click(object sender, EventArgs e)
        {
            Bitmap toSave = new Bitmap(this.pictureBoxOriginal.Image);
            SaveFileDialog save = new SaveFileDialog();
            save.ShowDialog();
            toSave.Save(save.FileName + ".bmp");
        }

        private void btnSaveResult_Click(object sender, EventArgs e)
        {
            Bitmap toSave = new Bitmap(this.pictureBoxResult.Image);
            SaveFileDialog save = new SaveFileDialog();
            save.ShowDialog();
            toSave.Save(save.FileName + ".bmp");
        }
    }
}
